﻿namespace CachePage
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Web;
    using System.Xml;

    public class ExtensionsConfigSection : ConfigurationSection
    {
        public List<string> CachePageAdapterAddPath = new List<string>();
        public List<string> CachePageAdapterRemovePath = new List<string>();
        public List<string> CachePageAdapterUpdateTriggersPath = new List<string>();
        public List<string> CachePageScriptAddPath = new List<string>();
        public TimeSpan CacheTimeOut;

        protected override void DeserializeSection(XmlReader reader)
        {
            XmlDocument document = new XmlDocument();
            document.Load(reader);
            XmlElement xmlElement = document.DocumentElement["cachePageAdapter"];
            if (xmlElement != null)
            {
                this.GetCachePageAdapterList(xmlElement);
            }
            xmlElement = document.DocumentElement["cachePageScript"];
            if (xmlElement != null)
            {
                this.GetCachePageScriptList(xmlElement);
            }
        }

        public static List<string> GetCachePageAdapterAddPath()
        {
            return CacheUtility<List<string>>.GetByContext("Extensions_CachePageAdapterAddPath", ConfigPath, new CacheUtility<List<string>>.NullCase(ExtensionsConfigSection.GetNullCachePageAdapterAddPath));
        }

        private void GetCachePageAdapterList(XmlElement xmlElement)
        {
            if (xmlElement.Attributes["timeout"] != null)
            {
                int num;
                if (int.TryParse(xmlElement.Attributes["timeout"].InnerText, out num))
                {
                    int hours = num / 60;
                    int minutes = num % 60;
                    this.CacheTimeOut = new TimeSpan(hours, minutes, 0);
                }
                else
                {
                    this.CacheTimeOut = new TimeSpan(0, 40, 0);
                }
            }
            foreach (XmlNode node in xmlElement.ChildNodes)
            {
                if (node.Attributes["path"] != null)
                {
                    if (node.Name == "add")
                    {
                        bool flag;
                        this.CachePageAdapterAddPath.Add(node.Attributes["path"].InnerText);
                        if (!(((node.Attributes["updateTriggers"] == null) || !bool.TryParse(node.Attributes["updateTriggers"].InnerText, out flag)) || flag))
                        {
                            this.CachePageAdapterUpdateTriggersPath.Add(node.Attributes["path"].InnerText);
                        }
                    }
                    else if (node.Name == "remove")
                    {
                        this.CachePageAdapterRemovePath.Add(node.Attributes["path"].InnerText);
                    }
                }
            }
        }

        public static List<string> GetCachePageAdapterRemovePath()
        {
            return CacheUtility<List<string>>.GetByContext("Extensions_CachePageAdapterRemovePath", ConfigPath, new CacheUtility<List<string>>.NullCase(ExtensionsConfigSection.GetNullCachePageAdapterRemovePath));
        }

        public static List<string> GetCachePageAdapterUpdateTriggersPath()
        {
            return CacheUtility<List<string>>.GetByContext("Extensions_CachePageAdapterUpdateTriggersPath", ConfigPath, new CacheUtility<List<string>>.NullCase(ExtensionsConfigSection.GetNullCachePageAdapterUpdateTriggersPath));
        }

        public static List<string> GetCachePageScriptList()
        {
            return CacheUtility<List<string>>.GetByContext("Extensions_CachePageScriptList", ConfigPath, new CacheUtility<List<string>>.NullCase(ExtensionsConfigSection.GetNullCachePageScriptList));
        }

        private void GetCachePageScriptList(XmlElement xmlElement)
        {
            foreach (XmlNode node in xmlElement.ChildNodes)
            {
                if ((node.Attributes["path"] != null) && (node.Name == "add"))
                {
                    this.CachePageScriptAddPath.Add(node.Attributes["path"].InnerText);
                }
            }
        }

        public static TimeSpan GetCacheTimeOut()
        {
            return CacheUtility<TimeSpan>.GetByContext("Extensions_CacheTimeOut", ConfigPath, new CacheUtility<TimeSpan>.NullCase(ExtensionsConfigSection.GetNullCacheTimeOut));
        }

        public static List<string> GetNullCachePageAdapterAddPath(string key)
        {
            if (HasExtensionsConfigSection())
            {
                ExtensionsConfigSection section = (ExtensionsConfigSection) ConfigurationManager.GetSection("extensions");
                return section.CachePageAdapterAddPath;
            }
            return new List<string>();
        }

        public static List<string> GetNullCachePageAdapterRemovePath(string key)
        {
            if (HasExtensionsConfigSection())
            {
                ExtensionsConfigSection section = (ExtensionsConfigSection) ConfigurationManager.GetSection("extensions");
                return section.CachePageAdapterRemovePath;
            }
            return new List<string>();
        }

        public static List<string> GetNullCachePageAdapterUpdateTriggersPath(string key)
        {
            if (HasExtensionsConfigSection())
            {
                ExtensionsConfigSection section = (ExtensionsConfigSection) ConfigurationManager.GetSection("extensions");
                return section.CachePageAdapterUpdateTriggersPath;
            }
            return new List<string>();
        }

        public static List<string> GetNullCachePageScriptList(string key)
        {
            if (HasExtensionsConfigSection())
            {
                ExtensionsConfigSection section = (ExtensionsConfigSection) ConfigurationManager.GetSection("extensions");
                return section.CachePageScriptAddPath;
            }
            return new List<string>();
        }

        private static TimeSpan GetNullCacheTimeOut(string key)
        {
            if (HasExtensionsConfigSection())
            {
                ExtensionsConfigSection section = (ExtensionsConfigSection) ConfigurationManager.GetSection("extensions");
                return section.CacheTimeOut;
            }
            return TimeSpan.Zero;
        }

        private static bool GetNullHasExtensionsConfigSection(string key)
        {
            return (ConfigurationManager.GetSection("extensions") != null);
        }

        public static bool HasExtensionsConfigSection()
        {
            return CacheUtility<bool>.GetByContext("Extensions_ConfigSection", ConfigPath, new CacheUtility<bool>.NullCase(ExtensionsConfigSection.GetNullHasExtensionsConfigSection));
        }

        public static string ConfigPath
        {
            get
            {
                object obj2 = HttpContext.Current.Application["Extensions_ConfigPath"];
                if (obj2 == null)
                {
                    obj2 = HttpContext.Current.Server.MapPath("~/web.config");
                    HttpContext.Current.Application["Extensions_ConfigPath"] = obj2;
                }
                return obj2.ToString();
            }
        }
    }
}

